package com.wxj.book.controller;

import com.github.pagehelper.PageInfo;
import com.wxj.book.common.Const;
import com.wxj.book.pojo.Book;
import com.wxj.book.pojo.Category;
import com.wxj.book.pojo.Comment;
import com.wxj.book.pojo.User;
import com.wxj.book.service.BookService;
import com.wxj.book.service.BorrowVoService;
import com.wxj.book.service.CategoryService;
import com.wxj.book.service.CommentService;
import com.wxj.book.vo.BorrowVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class BookController {
    @Autowired
    private BookService bookService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private BorrowVoService borrowVoService;
    @Autowired
    private CommentService commentService;

    /**
     * 跳转到图书详情界面
     */
    @RequestMapping("book/bookDetail")
    public String bookDetail(Model model,
                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                             @RequestParam("bookId") Integer bookId,
                             HttpServletRequest request) {
        // 查询所有的分类
        List<Category> categoryList = categoryService.getAll();
        model.addAttribute("categoryList", categoryList);
        // 根据书本id查询书本详细信息
        if (bookId.equals(" ") || bookId == null) {
            return "404";
        }
        // 去查询图书详细信息
        Book book = bookService.getByBookId(bookId);
        if (book == null) {
            return "404";
        }
        model.addAttribute("book",book);
        // 根据分类查出相关的图书
        List<Book> books = bookService.getByCategoryId(book.getCategoryId());
        if (books.size() == 0 || books == null) {
            return "404";
        }
        // 查询我的未归还记录
        User user = (User) request.getSession(true).getAttribute(Const.CURRENT_USER);
        if (user != null) {
            List<BorrowVo> borrowVos = borrowVoService.getByStuNum(user.getStuNum());
            model.addAttribute("borrowVos", borrowVos);
            // 查询用户是否借阅过该图书
            Comment comment = commentService.getByStuNum(user.getStuNum());
            model.addAttribute("comment", comment);
        }
        model.addAttribute("books", books);
        // 查询评论
        PageInfo<Comment> comments = commentService.getByBookId(pageNum, pageSize, bookId);
        model.addAttribute("comments", comments);
        return "bookDetail";
    }

    /**
     * 根据分类查询集合
     */
    @RequestMapping("book/getList")
    public String getByCategoryId(Model model,
                                  @RequestParam("categoryId") Integer categoryId,
                                  @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize,
                                  @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                  HttpServletRequest request) {
        // 查询所有的分类
        List<Category> categoryList = categoryService.getAll();
        model.addAttribute("categoryList", categoryList);
        // 查询分类名称
        Category category = categoryService.getCategoryName(categoryId);
        model.addAttribute("category", category);
        // 根据分类id查询相关书籍
        PageInfo<Book> pageInfo = bookService.getAllByCategoryId(pageNum, pageSize, categoryId);
        model.addAttribute("pageInfo", pageInfo);
        // 查询我的未归还记录
        User user = (User) request.getSession(true).getAttribute(Const.CURRENT_USER);
        if (user != null) {
            List<BorrowVo> borrowVos = borrowVoService.getByStuNum(user.getStuNum());
            model.addAttribute("borrowVos", borrowVos);
        }
        return "bookList";
    }
}
